using Autofac;
using Autofac.Extensions.DependencyInjection;
using Domain.Services;
using Infrastructure;
using Infrastructure.Repositories;
using TestDDD.Config;
using TestDDD.Extensions;
using TestDDD.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

IConfigurationSection configurationSection = builder.Configuration.GetSection(AppSettings.AppSetting);
builder.Services.Configure<AppSettings>(configurationSection);
AppSettings appSettings = configurationSection.Get<AppSettings>();

#region Autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory())
    .ConfigureContainer<ContainerBuilder>(containerbuilder =>
    {
        containerbuilder.RegisterModule(new APIAutofacRegister());
        containerbuilder.RegisterModule(new InfrastructureAutofacRegister(appSettings.ConnectionStrings));
        containerbuilder.RegisterModule(new DomainServicesAutofacRegister());
    });
#endregion

builder.Services.AddAutoMapperSetup();
//builder.Services.AddSqlSugarSetup(appSettings.ConnectionStrings);
//builder.Services.AddDomainServicesSetup();
//builder.Services.AddRepositoriesSetup();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
